package cn.sptpc.service.impl;

import cn.hutool.core.util.PageUtil;
import cn.sptpc.dao.ActivityDao;
import cn.sptpc.dao.impl.ActivityDaoI;
import cn.sptpc.entity.java.Activity;
import cn.sptpc.entity.java.User;
import cn.sptpc.entity.web.JActivityAllEntity;
import cn.sptpc.entity.web.JMsgEntity;
import cn.sptpc.service.ActivityService;
import cn.sptpc.utils.FileUtils;
import cn.sptpc.utils.ModifyImg;
import com.google.gson.Gson;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

public class ActivityServiceI implements ActivityService {
    private Gson gson = new Gson();
    private ActivityDao dao = new ActivityDaoI();
    private JMsgEntity entity = new JMsgEntity();
    /**
     * 分页获取活动
     *
     * @param request
     * @return
     */
    @Override
    public String queryActivity(HttpServletRequest request) {
        JActivityAllEntity entity = new JActivityAllEntity();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int[] pageUtils = PageUtil.transToStartEnd(page, limit);
        pageUtils[1] = limit;
        User user = (User) request.getSession().getAttribute("user");
        List<Activity> activities = dao.queryAllUser(pageUtils, user);
        if (activities!= null){
            entity.setCode(0);
            entity.setCount(activities.size());
            entity.setData(activities);
        }else {
            entity.setCode(1);
            entity.setMsg("未知错误！！");
        }
        System.out.println(gson.toJson(entity));
        return gson.toJson(entity);
    }

    /**
     * 上传文件
     *
     * @param request
     * @return
     */
    @Override
    public String fileUpload(HttpServletRequest request) {
        InputStream in = FileUtils.uploadFile(request);
        String s = FileUtils.saveNative(in,  "D:/OASystem/word/native");
        FileInputStream ins = null;
        try {
            ins = new FileInputStream(new File("D:/OASystem/word/native/"+s));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String docImg = FileUtils.saveWordToImg(ins, "D:/OASystem/word/img");
        JMsgEntity msg = new JMsgEntity();
        if (docImg != null){
            request.getSession().setAttribute("docImg",docImg);
            msg.setCode(0);
        }else {
            msg.setCode(1);
            msg.setMsg("上传失败");
        }
        return gson.toJson(msg);
    }

    /**
     * 添加活动
     *
     * @param request
     * @return
     */
    @Override
    public String insertActivity(HttpServletRequest request) {
        String activityName = request.getParameter("activityName");
        String holdingDay = request.getParameter("holdingDay");
        String department = request.getParameter("department");
        String submittedDay = String.valueOf(System.currentTimeMillis());
        Long distribute = 1l;
        User user = (User) request.getSession().getAttribute("user");
        int subDepId = (int)user.getDepartmentId();
        long subuser = user.getId();
        String docImg = (String) request.getSession().getAttribute("docImg");
        Activity activity = new Activity();
        System.out.println(activityName);
        activity.setName(activityName);
        activity.setHoldingDay(holdingDay);
        activity.setSubmitUserId(subuser);
        activity.setSubDepId(subDepId);
        activity.setSubmittedDay(submittedDay);
        activity.setDistribute(distribute);
        activity.setDocpath(docImg);
        activity.setDepId(Integer.parseInt(department));
        int i = dao.insertActivity(activity);
        if (i == 1){
            entity.setCode(0);
            entity.setMsg("添加成功");
        }else {
            entity.setCode(1);
            entity.setMsg("添加失败");
        }
        return gson.toJson(entity);
    }

    /**
     * 活动信息
     *
     * @param request
     * @return
     */
    @Override
    public String activityInfo(HttpServletRequest request) {
        JActivityAllEntity entity = new JActivityAllEntity();
        String id = request.getParameter("id");
        Activity activity = dao.selectActivity(id);
        if (activity != null){
            entity.setCode(0);
            entity.setActivity(activity);
            entity.setMsg("请求成功");
        }else {
            entity.setCode(1);
            entity.setMsg("请求s失败");
        }
        return gson.toJson(entity);
    }

    /**
     * 模糊查询用户
     *
     * @param request
     * @return
     */
    @Override
    public String selectActivity(HttpServletRequest request) {
        Gson gson = new Gson();
        JActivityAllEntity activityAllEntity = new JActivityAllEntity();
        String key = request.getParameter("key");
        User user = (User) request.getSession().getAttribute("user");
        List<Activity> activities = dao.queryActivity(key, user);
        if (activities.size() < 1){
            activityAllEntity.setCode(1);
            activityAllEntity.setMsg("未找到该用户！");
        }
        activityAllEntity.setCode(0);
        activityAllEntity.setMsg("查询成功");
        activityAllEntity.setData(activities);
        return gson.toJson(activityAllEntity);
    }

    /**
     * 删除一个活动
     *
     * @param request
     * @return
     */
    @Override
    public String deleteActivity(HttpServletRequest request) {
        JActivityAllEntity activityAllEntity = new JActivityAllEntity();
        User user = (User) request.getSession().getAttribute("user");
        Long id = Long.parseLong(request.getParameter("id"));
        Integer i = dao.deleteActivity(id);
        if (i >0){
            activityAllEntity.setCode(0);
            activityAllEntity.setMsg("删除成功");
        }else {
            activityAllEntity.setCode(1);
            activityAllEntity.setMsg("删除失败");
        }
        return gson.toJson(activityAllEntity);
    }

    /**
     * 获取图片
     * @param request
     * @param response
     * @return
     */
    @Override
    public String getImg(HttpServletRequest request, HttpServletResponse response) {
        Activity a = dao.selectActivity(request.getParameter("id"));
        String docname = a.getDocpath();
        try {
            FileUtils.downloadFile("D:\\OASystem\\word\\img",docname,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过审核
     *
     * @param request
     * @return
     */
    @Override
    public String pass(HttpServletRequest request) {
        String id = request.getParameter("id");
        Activity activity = dao.selectActivity(id);
        JActivityAllEntity entity = new JActivityAllEntity();
        if (activity != null){
            String docpath = activity.getDocpath();
            String path = request.getSession().getServletContext().getRealPath("")+"img/pass.png";
            ModifyImg.pressImage(path,"D:/OASystem/word/img/"+docpath);
            activity.setPassDay(String.valueOf(System.currentTimeMillis()));
            activity.setDistribute(0);
            Integer i = dao.updateActivity(activity);
            if (i == 1){
                entity.setMsg("审核成功");
                entity.setCode(0);
            }else {
                entity.setMsg("审核失败");
                entity.setCode(1);
            }
        }
        return gson.toJson(entity);
    }

    /**
     * 拒绝审核
     *
     * @param request
     * @return
     */
    @Override
    public String refused(HttpServletRequest request) {
        String id = request.getParameter("id");
        Activity activity = dao.selectActivity(id);
        JActivityAllEntity entity = new JActivityAllEntity();
        if (activity != null){
            String docpath = activity.getDocpath();
            String path = request.getSession().getServletContext().getRealPath("")+"img/refused.png";
            ModifyImg.pressImage(path,"D:/OASystem/word/img/"+docpath);
            activity.setPassDay(String.valueOf(System.currentTimeMillis()));
            activity.setDistribute(2);
            Integer i = dao.updateActivity(activity);
            if (i == 1){
                entity.setMsg("审核成功");
                entity.setCode(0);
            }else {
                entity.setMsg("审核失败");
                entity.setCode(1);
            }
        }
        return gson.toJson(entity);
    }
}
